package com.kgc.est.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.est.dto.ModelDto;
import com.kgc.est.mapper.ModelMapper;
import com.kgc.est.pojo.Model;
import com.kgc.est.request.ModelReq;
import com.kgc.est.utils.Result;
import com.kgc.est.utils.ResultUtils;
import com.kgc.est.utils.oss.OssConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/model")
public class ModelServiceImpl {

    Logger log = LoggerFactory.getLogger(ModelServiceImpl.class);

    @Autowired(required = false)
    private ModelMapper modelMapper;

    @Autowired
    private OssConfig ossConfig;

    @PostMapping("/searchModel")
    public Result searchModel(@RequestBody ModelReq modelReq){
        log.info("服务提供者拿到的搜索商品的入参：{}",modelReq);

        //实现查询业务
        Page<Model> page = new Page<Model>(modelReq.getPageIndex(),modelReq.getPageSize());

        //创建wapper
        QueryWrapper<Model> qw = new QueryWrapper<Model>();

        //拼接查询条件
        if (null != modelReq.getBrandId()){
            qw.eq("brandId",modelReq.getBrandId());
        }

        if (null != modelReq.getClassifyId()){
            qw.eq("classifyId",modelReq.getClassifyId());
        }

        if (null != modelReq.getModelName()){
            qw.like("modelName",modelReq.getModelName());
        }

        qw.eq("status",1);
        qw.eq("isDelete",0);
        //进行分页查询
        modelMapper.selectPage(page,qw);

        //使用map封装返回值
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo",page.getCurrent());
        map.put("pageSize",page.getSize());
        map.put("total",page.getTotal());
        map.put("pages",page.getPages());

        List<Model> records = page.getRecords();
        //创建一个list存储商品信息
        List<ModelDto> list = new ArrayList<ModelDto>();

        for (Model record : records) {
            ModelDto modelDto = new ModelDto(record);

            //引用阿里OSS存储的图片地址
            modelDto.setFaceImg(ossConfig.getOssWebUrl()+record.getFaceImg());
            modelDto.setContentImg(ossConfig.getOssWebUrl()+record.getContentImg());
            list.add(modelDto);
        }

        map.put("list",list);


        return ResultUtils.returnDataSuccess(map);
    }

    @GetMapping("{id}/detail")
    Result<ModelDto> detail(@PathVariable("id")Long id){

        log.info("服务提供者拿到的商品id：{}",id);
        //到mapper层根据id查询商品类型信息
        Model model = modelMapper.selectById(id);

        //根据查询到的数据封装到Dto中
        ModelDto modelDto = new ModelDto(model);
        modelDto.setFaceImg(ossConfig.getOssWebUrl()+model.getFaceImg());
        modelDto.setContentImg(ossConfig.getOssWebUrl()+model.getContentImg());

        return ResultUtils.returnDataSuccess(modelDto);
    }

}
